﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Kenly.DBFramework.Tests
{


    /// <summary>
    ///这是 OrderTest 的测试类，旨在
    ///包含所有 OrderTest 单元测试
    ///</summary>
    [TestClass()]
    public class OrderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///Order 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void OrderConstructorDirectionTest()
        {
            string msg = "Property Order.IsValid did not set correctly.";

            string orderColumn = "Empty";
            OrderDirection direction = new OrderDirection();
            Order target = new Order(orderColumn, direction);

            Assert.IsTrue(target.IsValid, msg);
            Assert.IsTrue(target.OrderDirection == direction, msg);
        }

        /// <summary>
        ///Order 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void OrderConstructorColumnTest()
        {
            string msg = "Property Order.IsValid did not set correctly.";

            string orderColumn = "ColumnName";
            Order target = new Order(orderColumn);

            Assert.IsTrue(target.IsValid, msg);
            Assert.IsTrue(target.OrderDirection == OrderDirection.Ascend, msg);
        }

        /// <summary>
        ///Order 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void OrderConstructorTest()
        {
            string msg = "Property Order.IsValid did not set correctly.";

            Order target = new Order();

            Assert.IsFalse(target.IsValid, msg);
        }


        /// <summary>
        ///Ascend 的测试
        ///</summary>
        [TestMethod()]
        public void AscendTest()
        {
            string orderColumn = "ColumnName";
            Order target = Order.Ascend(orderColumn);

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(target.OrderDirection == OrderDirection.Ascend);
            Assert.IsTrue(target.OrderColumn == orderColumn);
        }

        /// <summary>
        ///Ascend 的测试
        ///</summary>
        [TestMethod()]
        public void DescendTest()
        {
            string orderColumn = "ColumnName";
            Order target = Order.Descend(orderColumn);

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(target.OrderDirection == OrderDirection.Descend);
            Assert.IsTrue(target.OrderColumn == orderColumn);
        }

        /// <summary>
        ///Ascend 的测试
        ///</summary>
        [TestMethod()]
        public void BitwiseAndTest()
        {
            Order left = CreateLeft();
            Order right = CreateRight();
            Order target = left & right;

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(!string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(target.OrderString == target.ToString());

            target = Order.BitwiseAnd(left, right);

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(!string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(target.OrderString == target.ToString());

            //right only.
            target = Order.BitwiseAnd(new Order(), right);

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(target == right);

            //left only.
            target = Order.BitwiseAnd(left, new Order());

            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(target == left);

            target = Order.BitwiseAnd(new Order(), new Order());

            Assert.IsFalse(target.IsValid);
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderString));
        }

        private static Order CreateRight()
        {
            return Order.Descend("DescendColumnName");
        }

        private static Order CreateLeft()
        {
            return Order.Ascend("AscendColumnName");
        }


        /// <summary>
        ///ToString 的测试
        ///</summary>
        [TestMethod()]
        public void ToStringTest()
        {
            Order left = CreateLeft();
            Order right = CreateRight();
            Order target = left & right;

            Assert.IsTrue(target.OrderString == target.ToString());

            target = new Order();
            Assert.IsFalse(target.IsValid);
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(string.IsNullOrEmpty(target.ToString()));
        }

        /// <summary>
        ///ClearOrder 的测试
        ///</summary>
        [TestMethod()]
        public void ClearOrderTest()
        {
            Order target = CreateLeft();

            target.ClearOrder();

            Assert.IsFalse(target.IsValid);
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(string.IsNullOrEmpty(target.ToString()));
        }

        /// <summary>
        ///Clear 的测试
        ///</summary>
        [TestMethod()]
        public void ClearTest()
        {
            Order target = CreateLeft();

            target.Clear();

            Assert.IsFalse(target.IsValid);
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderColumn));
            Assert.IsTrue(string.IsNullOrEmpty(target.OrderString));
            Assert.IsTrue(string.IsNullOrEmpty(target.ToString()));
        }

        /// <summary>
        ///CompareTo 的测试
        ///</summary>
        [TestMethod()]
        public void CompareToTest()
        {
            Order left = CreateLeft();
            Order right = CreateRight();

            Assert.IsTrue(left.CompareTo(null) == -1);
            Assert.IsTrue(left.CompareTo(new Order()) == -1);
            Assert.IsTrue(left.CompareTo(left) == 0);
            Assert.IsTrue(left.CompareTo(right) == left.OrderColumn.CompareTo(right.OrderColumn));
        }

    }
}
